﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
using DevExpress.XtraCharts;

namespace BI
{
    public partial class UcOneSeriesChart : UserControl
    {
        private DataTable _Data = new DataTable();
        public DataTable Data
        {
            get
            {
                return _Data;
            }
            set
            {
                _Data = value;
            }
        }

        public UcOneSeriesChart()
        {
            DataColumn column1 = new DataColumn("Series1Name", System.Type.GetType("System.String"));
            Data.Columns.Add(column1);

            DataColumn column2 = new DataColumn("Series1Value", System.Type.GetType("System.Decimal"));
            Data.Columns.Add(column2);
        }

        //public void SetParams(string title, DateTime startTime, DateTime finishTime, TimeSpan timeSpan)
        //{
        //     InitializeComponent();
        //    _Title = title;
        //}

        public void GenChart(string seriesName, ViewType viewType, string title)
        {
            ChartControl chart = new ChartControl();
            Series series1 = new Series(seriesName, viewType);
            chart.Series.Add(series1);
            chart.PaletteName = "Aspect";

            if (viewType == ViewType.Pie)
            {
                foreach (DataRow row in _Data.Rows)
                {
                    series1.Points.Add(new SeriesPoint((string)row["Series1Name"], (decimal)row["Series1Value"]));
                }

                PiePointOptions options = series1.PointOptions as PiePointOptions;
                options.PercentOptions.ValueAsPercent = false ;

                PieSeriesLabel label = chart.Series[0].Label as PieSeriesLabel;
                if (label != null)
                {
                    label.Position = PieSeriesLabelPosition.Radial;
                    label.LineVisible = true;
                    label.TextColor = (label.Position == PieSeriesLabelPosition.Outside || label.Position == PieSeriesLabelPosition.TwoColumns) ? Color.Empty : Color.Black;
                    label.Antialiasing = true;
                }
                label.Visible = true;
                series1.PointOptions.PointView = PointView.SeriesName;
                PieSeriesView view = series1.View as PieSeriesView;
                view.ExplodeMode = PieExplodeMode.All;
                series1.PointOptions.Pattern = "{A}:{V}";

                ChartTitle chartTitle = new ChartTitle();
                chartTitle.Text = title;
                chart.Titles.Add(chartTitle);
                chart.Legend.Visible = true;
            }
            else
            {
                foreach (DataRow row in _Data.Rows)
                {
                    series1.Points.Add(new SeriesPoint((string)row["Series1Name"], (decimal)row["Series1Value"]));
                }
                ChartTitle chartTitle = new ChartTitle();
                chartTitle.Text = title;
                chart.Titles.Add(chartTitle);
                chart.Legend.Visible = false;
            }

            for (int i = 0; i < this.Controls.Count; i++) this.Controls.RemoveAt(0);

            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }
    }
}
